<?php


namespace app\admin\controller;


use app\model\ModelReceivable;
use easyadmin\app\columns\form\FormText;
use easyadmin\app\columns\lists\ListDateTime;
use easyadmin\app\columns\lists\ListText;
use easyadmin\app\libs\Btn;
use easyadmin\app\libs\ListField;
use easyadmin\app\libs\Page;
use easyadmin\app\libs\PageForm;
use easyadmin\app\libs\PageList;
use easyadmin\app\libs\Verify;
use think\db\Query;

class BankReceivable extends Admin
{
    protected $tableName = 'receivable';


    protected function configList(PageList $page)
    {
        //定义表格 行的 操作按钮
        $page
            ->addAction('状态更改', 'statusUpdate', [
                'icon' => 'layui-icon layui-icon-edit',
                'class' => ['layui-btn-primary', 'layui-btn-xs']
            ])
            ;

        //定义页面的添加按钮
        $addBtn = new Btn();
        $addBtn->setLabel('添加');
        $addBtn->setUrl('add');
        $addBtn->setIcon('layui-icon layui-icon-add-1');
        $page->setActionAdd($addBtn);

    }

    public function statusUpdate() {
        $id = input('id');
        $receivableModel = new ModelReceivable();
        $status = $receivableModel->find($id);
        if($status['status'] == 0) {
            $newStatus = 1;
        }else {
            $newStatus = 0;
        }
        $receivableModel->where(['id' => $id])->save(['status' => $newStatus ]);
        redirect(url('bankReceivable/lists'))->send();
    }

    protected function configListField(ListField $list)
    {
        $list
            ->addField('id', 'ID', ListText::class)
            ->addField('bank_name', '银行名称', ListText::class)
            ->addField('card_number', '银行卡号', ListText::class)
            ->addField('card_user_name', '账户户名', ListText::class)
            ->addField('amount_limit', '账户收款限制金额', ListText::class)
            ->addField('current_receive_amount', '当前账户收款金额', ListText::class)
            ->addField('manage.username', '创建人', ListText::class)
            ->addField('status', '账户状态', ListText::class, [
                'class' => 'receivable-account-status',
                'jsFiles' => ['/lib/index.js']
            ])
            ->addField('create_time', '创建时间', ListDateTime::class)
            ->addField('remark', '备注', ListDateTime::class)
            ;
    }

    protected function configFormField(PageForm $page)
    {
        $page
            ->addField('bank_name', '账户银行', FormText::class, [
                'required' => true,
                'verify' => (new Verify())->addRule('chinese', '输入银行中文名称')
            ])
            ->addField('card_user_name', '账户户名', FormText::class, [
                'required' => true,
            ])
            ->addField('card_number', '银行卡号', FormText::class, [
                'required' => true,
                'verify' => function($val) {
                    if(!preg_match("/^\d{10,20}$/",$val)) {
                        return '请输入正确的银行卡号';
                    }
                    return true;
                }
            ])
            ->addField('amount_limit', '收款限制金额', FormText::class, [
                'required' => true,
                'verify' => (new Verify())->addRule('float', '请输入正确的金额')
            ])
            ->addField('remark', '备注', FormText::class, [
//                'required' => true,
            ]);
    }

    protected function configListWhere(Page $page, Query $query, $alias)
    {
        $query->where(['type' => 101]);//查询银行101类型
    }

    protected function configListJoin(Page $page, Query $query, string $alias)
    {
        $query->join('manage ', 'manage.id='. $alias .'.create_user_id');
    }



    protected function insertBefore($data): array
    {
        $data['type'] = 101;//类型为银行卡
        $data['status'] = 1;
        $data['create_user_id'] = $this->getLoginUserId();
        $data['create_time'] = time();
        return $data;
    }
}